package nyist.media.service.impl;

import nyist.media.bean.admin.AdminListBean;
import nyist.media.entity.ApplyRecord;
import nyist.media.entity.admin.Admin;
import nyist.media.entity.admin.Role;
import nyist.media.exception.BusinessException;
import nyist.media.page.ApiResult;
import nyist.media.page.AppPage;
import nyist.media.repository.AdminRepository;
import nyist.media.repository.ApplyRecordRepository;
import nyist.media.repository.RoleRepository;
import nyist.media.service.TeacherService;
import nyist.media.vo.ApplyRecordVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 教师模块
 * author:   dcf
 * date:     2019/3/17 11:52
 */
@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private AdminRepository adminRepository;
    @Autowired
    private RoleRepository roleRepository;
    @Autowired
    private ApplyRecordRepository applyRecordRepository;

    @Override
    public AppPage<AdminListBean> findPage(PageRequest pageRequest, String keyword) {
        Role teacherRole = roleRepository.findByNameLike("%教师%");
        Role adminRole = roleRepository.findByNameLike("%系统管理员%");
        if (Objects.isNull(teacherRole)) {
            throw new BusinessException(ApiResult.DATA_NOT_FOUND, "角色信息不存在");
        }
        List<Admin> lists = adminRepository.findAll();
        List<Long> ids = lists.stream().filter(admin -> admin.getRoles().contains(teacherRole) && !admin.getRoles().contains(adminRole)).map(Admin::getId).collect(Collectors.toList());
        if (ids.isEmpty()) {
            return new AppPage<>(pageRequest, null, null, Collections.emptyList());
        }
        Page<Admin> page = adminRepository.findAll(new Specification<Admin>() {
            @Override
            public Predicate toPredicate(Root<Admin> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                CriteriaBuilder.In<Object> in = criteriaBuilder.in(root.get("id"));
                for (Long id : ids) {
                    in.value(id);
                }
                predicateList.add(in);
                Predicate[] predicates = new Predicate[predicateList.size()];
                return criteriaBuilder.and(predicateList.toArray(predicates));
            }
        }, pageRequest);
        List<AdminListBean> teacherList = page.getContent().stream().map(AdminListBean::new).collect(Collectors.toList());
        return new AppPage<>(pageRequest, page.getTotalElements(), page.getTotalPages(), teacherList);
    }

    @Override
    public AppPage<ApplyRecordVO> findApplyOrders(PageRequest pageRequest, Long id) {
        Admin teacherAdmin = adminRepository.findOne(id);
        ApplyRecord applyRecord = new ApplyRecord();
        applyRecord.setAdmin(teacherAdmin);
        Page<ApplyRecord> page = applyRecordRepository.findAll(Example.of(applyRecord), pageRequest);
        List<ApplyRecordVO> recordVOS = page.getContent().stream().map(ApplyRecordVO::new).collect(Collectors.toList());
        return new AppPage<>(pageRequest, page.getTotalElements(), page.getTotalPages(), recordVOS);
    }
}

